package com.finger.forest;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.finger.forest.adapter.Adapter;
import com.finger.forest.base.BaseActivity;
import com.finger.forest.base.BaseView;
import com.finger.forest.base.CommonKey;
import com.finger.forest.base.ResponseTag;
import com.finger.forest.custom.MyDialog;
import com.finger.forest.http.presenter.RegisterPresenter;
import com.finger.forest.model.ContactModel;
import com.finger.forest.model.FzFireModel;
import com.finger.forest.utils.ClickUtils;
import com.finger.forest.utils.ToastUtils;
import com.finger.forest.utils.encode.PrefsUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

public class ContactActivity extends BaseActivity<RegisterPresenter> implements BaseView {

    /**
     * 返回
     */
    @BindView(R.id.btn_back)
    ImageView backIv;

    /**
     * 右边
     */
    @BindView(R.id.btn_right)
    TextView rightBtn;

    @BindView(R.id.recyclerView)
    RecyclerView mRecyclerView;
    private Adapter mAdapter;

    private MyDialog myDialog;

    private List<ContactModel> contactList = new ArrayList<>();

    @Override
    protected int setView() {
        return R.layout.activity_contact;
    }

    @Override
    protected void receiveData() {

    }

    @Override
    protected void initializeView() {

    }

    @Override
    protected void loadDataFromServer() {
        queryList();
    }

    private void queryList(){
        String userId = PrefsUtil.getString(this, CommonKey.USER_ID);
        Map<String, Object> maps = new HashMap<>();
        maps.put("gridId", userId);
        presenter.queryContactList(maps);//查询联系人列表
    }

    public void deleteItem(String id){
        Map<String, Object> maps = new HashMap<>();
        maps.put("id", id);
        presenter.contactDelete(maps);//联系人删除
    }

    /**
     * 设置适配器方法
     */
    private void setAdapter(List<ContactModel> contactList) {
        //设置布局管理器
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        //设置适配器
        mRecyclerView.setAdapter(mAdapter = new Adapter(this, contactList));
        //设置列表中子项的动画
        mRecyclerView.setItemAnimator(new DefaultItemAnimator());
    }

    @OnClick({R.id.btn_back, R.id.btn_right })
    public void onClick(View v) {
        if (ClickUtils.isFastClick()) { //防止重复点击
            switch (v.getId()) {

                //返回
                case R.id.btn_back:
                    finish();
                    break;

                //右边按钮
                case R.id.btn_right:
                    //弹框
                    myDialog=new MyDialog(ContactActivity.this,R.style.dialogFullStyle);
                    myDialog.setTitle("添加联系人");
                    LinearLayout layoutHotContent = (LinearLayout) LayoutInflater.from(ContactActivity.this).inflate(R.layout.dialog_content_contact, null);

                    myDialog.setYesOnclickListener("保存", new MyDialog.onYesOnclickListener() {
                        @Override
                        public void onYesOnclick() {
                            TextView nameTv = layoutHotContent.findViewById(R.id.name);
                            TextView cellphoneTv = layoutHotContent.findViewById(R.id.cellphone);
                            String name = nameTv.getText().toString();
                            String cellphone = cellphoneTv.getText().toString();
                            if (name.isEmpty()){
                                ToastUtils.show("姓名不能为空");
                                return;
                            }
                            if (cellphone.isEmpty()){
                                ToastUtils.show("号码不能为空");
                                return;
                            }

                            String userId = PrefsUtil.getString(ContactActivity.this, CommonKey.USER_ID);
                            Map<String, Object> maps = new HashMap<>();
                            maps.put("gridId", userId);
                            maps.put("name", name);
                            maps.put("phone", cellphone);
                            presenter.addContact(maps);//添加联系人

                            myDialog.dismiss();
                        }
                    });
                    myDialog.setNoOnclickListener("取消", new MyDialog.onNoOnclickListener() {
                        @Override
                        public void onNoClick() {
                            myDialog.dismiss();
                        }
                    });

                    myDialog.setLayout(layoutHotContent);
                    myDialog.show();
                    break;

                default:
                    break;
            }
        }
    }

    @Override
    public void onSuccess(Object model, int tag, String msg) {
        switch (tag) {

            //初始化联系人
            case ResponseTag.QUERY_CONTACT_LIST:
                contactList = (List<ContactModel>) model;
                setAdapter(contactList);//调用设置适配器方法
                break;

            //删除联系人
            case ResponseTag.CONTACT_DELETE:
                ToastUtils.show(msg);
                mAdapter.notifyDataSetChanged();
                break;

            //新增联系人
            case ResponseTag.ADD_CONTACT:
                ToastUtils.show(msg);
                queryList();
                break;

            default:
                break;
        }
    }

    @Override
    public void onError(int errcode, int tag, String message) {

    }
}
